package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper;

    @Autowired
    private RoomLabelMapper roomLabelMapper;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;

    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private GraphInfoService graphInfoService;


    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> itemVoIPage, RoomQueryVo queryVo) {
        IPage<RoomItemVo> iPage = baseMapper.pageItem(itemVoIPage, queryVo);
        return iPage;
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        // 房间数据
        RoomInfo roomInfo = baseMapper.selectById(id);

        // 公寓数据
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        // 图片
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
        graphInfoQueryWrapper.eq("item_id", id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapper);
        List<GraphVo> graphVos = graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());

        // 配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

        // 标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);


        // 支付方式
        List<PaymentType> paymentType = paymentTypeMapper.selectListByRoomId(id);

        // 租期
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectListByRoomId(id);

        // 基本属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentType);
        roomDetailVo.setLeaseTermList(leaseTerms);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        return roomDetailVo;
    }

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean ifRoom = roomSubmitVo.getId() != null ? true : false;
        // 1 房间信息 2 公寓信息
        this.saveOrUpdate(roomSubmitVo);

        if (ifRoom) {
            //更新
            Long roomId = roomSubmitVo.getId();

            // 3 删除租期中间表
            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper.eq("room_id", roomId);
            roomLeaseTermMapper.delete(roomLeaseTermQueryWrapper);

            // 4 删除支付方式中间表
            QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq("room_id", roomId);
            roomPaymentTypeMapper.delete(roomPaymentTypeQueryWrapper);

            // 5 删除基本属性中间表
            QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
            roomAttrValueQueryWrapper.eq("room_id", roomId);
            roomAttrValueMapper.delete(roomAttrValueQueryWrapper);

            // 7 删除标签中间表
            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper.eq("room_id", roomId);
            roomLabelMapper.delete(roomLabelQueryWrapper);

            // 8 删除配套中间表
            QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
            roomFacilityQueryWrapper.eq("room_id", roomId);
            roomFacilityMapper.delete(roomFacilityQueryWrapper);

            // 6 删除图片中间表
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
            graphInfoQueryWrapper.eq("item_id", roomId);
            graphInfoMapper.delete(graphInfoQueryWrapper);
        }

        Long roomId = roomSubmitVo.getId();
        // 3 租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTerms = leaseTermIds.stream().map(leaseTermId -> {
            RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
            roomLeaseTerm.setRoomId(roomId);
            roomLeaseTerm.setLeaseTermId(leaseTermId);
            return roomLeaseTerm;
        }).collect(Collectors.toList());
        roomLeaseTermService.saveBatch(roomLeaseTerms);

        // 4 支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypes = paymentTypeIds.stream().map(paymentTypeId -> {
            return RoomPaymentType.builder().roomId(roomId).paymentTypeId(paymentTypeId).build();
        }).collect(Collectors.toList());
        roomPaymentTypeService.saveBatch(roomPaymentTypes);

        // 5 基本属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValues = attrValueIds.stream().map(attrValueId -> {
            return RoomAttrValue.builder().roomId(roomId).attrValueId(attrValueId).build();
        }).collect(Collectors.toList());
        roomAttrValueService.saveBatch(roomAttrValues);

        // 7 标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabels = labelInfoIds.stream().map(labelInfoId -> {
            return RoomLabel.builder().roomId(roomId).labelId(labelInfoId).build();
        }).collect(Collectors.toList());
        roomLabelService.saveBatch(roomLabels);

        // 8 配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilities = facilityInfoIds.stream().map(facilityInfoId -> {
            return RoomFacility.builder().roomId(roomId).facilityId(facilityInfoId).build();
        }).collect(Collectors.toList());
        roomFacilityService.saveBatch(roomFacilities);

        // 6 图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos = graphVoList.stream().map(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomId);
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            return graphInfo;
        }).collect(Collectors.toList());
        graphInfoService.saveBatch(graphInfos);


    }
    @Transactional
    @Override
    public void removeRoomById(Long id) {
        // 3 删除租期中间表
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
        roomLeaseTermQueryWrapper.eq("room_id", id);
        roomLeaseTermMapper.delete(roomLeaseTermQueryWrapper);

        // 4 删除支付方式中间表
        QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq("room_id", id);
        roomPaymentTypeMapper.delete(roomPaymentTypeQueryWrapper);

        // 5 删除基本属性中间表
        QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
        roomAttrValueQueryWrapper.eq("room_id", id);
        roomAttrValueMapper.delete(roomAttrValueQueryWrapper);

        // 7 删除标签中间表
        QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
        roomLabelQueryWrapper.eq("room_id", id);
        roomLabelMapper.delete(roomLabelQueryWrapper);

        // 8 删除配套中间表
        QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
        roomFacilityQueryWrapper.eq("room_id", id);
        roomFacilityMapper.delete(roomFacilityQueryWrapper);

        // 6 删除图片中间表
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
        graphInfoQueryWrapper.eq("item_id", id);
        graphInfoMapper.delete(graphInfoQueryWrapper);

        //删除房间
        baseMapper.deleteById(id);
    }
}




